<?php
/**
 * 用户管理控制器
 * @author juntao
 *
 */
class UserController extends App_Zend_Controller_Action_Admin
{
	public function init() 
	{
		parent::init();		
	}
	
	/**修改密码*/
	public function changepswAction()
	{
		$currentUser = App_User::getCurrentUser();
		if($this->getRequest()->isPost()) {			
			$truename = $this->getRequest()->getParam('truename', '');
			$password = $this->getRequest()->getParam('password', '');
			$confirmPassword = $this->getRequest()->getParam('confirm_password', '');		
			
			$user = App_Model::factory('User');
			
			$msg = array();	
			
			if(empty($password)) {
				$msg[] = '密码不能为空';
			} elseif ($password != $confirmPassword) {
				$msg[] = '密码不相同';
			}
			
			if ($msg) {
				$this->view->msg = $msg;	
				$this->view->user = $currentUser;
				return;
			}
			
			$row = array('user_pass'=>md5($password), 'true_name'=>$truename);
			
			if ($user->update($row, $currentUser->user_id)) {
				$msg[] = '修改成功';
			} else {
				$msg[] = '修改失败';
			}
			$this->view->msg = $msg;
		}
		
		$this->view->user = $currentUser; 
	}
	
	/**用户登录*/
	public function loginAction()
	{
		if ($this->getRequest()->isPost()) {
			$user = App_Model::factory('User');
			$userName = $this->getRequest()->getParam('name', '');
			$passWord = $this->getRequest()->getParam('pass', '');
			$useryz = $this->getRequest()->getParam('useryz', '');
			if(strtolower($_SESSION['administratorConfirmCode']) == strtolower($useryz)) {
				if ($user->login($userName, $passWord)) {
					$this->_redirect('/');
				}
			}
		}

		$user = App_User::getCurrentUser();
		if($user){
			$this->_redirect('/');
		}
	}
	
	/**用户退出*/
	public function logoutAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$userNameSpace = new Zend_Session_Namespace('User');
        
        if (!empty($userNameSpace->user)) {
           unset($userNameSpace->user);
        }
        $this->_redirect('/user/login');
	}
	
	/**验证码*/
	public function imageAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$num2 = rand(1,5);
		
		$fun = array('1'=>'image1','2'=>'image2', '3'=>'image3', '4'=>'image4','5'=>'image5');
        $this->$fun[$num2]();
	}

	/**
	 * 定义生成加法运算验证码图片
	 * @param int $w 图片宽
	 * @param int $h 图片高
	 */
	public function image1($w = 100, $h = 28)
	{
		session_start ();
		//定义头文件为图片
		header("Content-type: image/png");
		
		//生成验证码,创建图片设置字体颜色
		$im = imagecreate($w, $h);
		$red = imagecolorallocate($im, 255, 255, 255);
		$white = imagecolorallocate($im, 255, 255, 255);
		
		//随机生成两个数字
		$num1 = rand(1, 20);
		$num2 = rand(1, 20);
		$_SESSION ["administratorConfirmCode"] = $num1+$num2;
		
		//设置图片背景颜色
		$gray = imagecolorallocate($im, 118, 151, 199);
		$black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
		
		//创建图片背景
		imagefilledrectangle($im, 0, 0, 100, 24, $black);
		
		//在画布上随机生成大量点
		for ($i = 0; $i < 80; $i++) {
			imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
		}
		
		//将计算验证码写入到图片中
		imagestring($im, 5, 5, 4, $num1, $red);
		imagestring($im, 5, 30, 3, "+", $red);
		imagestring($im, 5, 45, 4, $num2, $red);
		imagestring($im, 5, 70, 3, "=", $red);
		imagestring($im, 5, 80, 2, "?", $white);
		
		//输出图片
		imagepng($im);
		imagedestroy($im);
	}
	
	/**
	 * 定义生成减法运算验证码图片
	 * @param int $w 图片宽
	 * @param int $h 图片高
	 */
	public function image2($w = 100, $h = 28)
	{
		session_start ();
		//定义头文件为图片
		header("Content-type: image/png");
	
		//生成验证码,创建图片设置字体颜色
		$im = imagecreate($w, $h);
		$red = imagecolorallocate($im, 255, 255, 255);
		$white = imagecolorallocate($im, 255, 255, 255);
	
		//随机生成两个数字
		$num1 = rand(1, 20);
		$num2 = rand(1, 20);
		$_SESSION ["administratorConfirmCode"] = $num1-$num2;
	
		//设置图片背景颜色
		$gray = imagecolorallocate($im, 118, 151, 199);
		$black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
	
		//创建图片背景
		imagefilledrectangle($im, 0, 0, 100, 24, $black);
	
		//在画布上随机生成大量点
		for ($i = 0; $i < 80; $i++) {
			imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
		}
	
		//将计算验证码写入到图片中
		imagestring($im, 5, 5, 4, $num1, $red);
		imagestring($im, 5, 30, 3, "-", $red);
		imagestring($im, 5, 45, 4, $num2, $red);
		imagestring($im, 5, 70, 3, "=", $red);
		imagestring($im, 5, 80, 2, "?", $white);
	
		//输出图片
		imagepng($im);
		imagedestroy($im);
	}
	
	/**
	  * 生成字符验证码图片
	  * @param int $length 验证码字符长度
	  */
	public function image3($length = '4', $w = 150, $y = 50)
	{
		session_start ();
		//设置文件头为图片输出
		Header("Content-type: image/JPEG");

		$str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234565789";
		$result="";
		for($i=0;$i<$length;$i++){
			$num[$i]=rand(0,61);
			$result.=$str[$num[$i]];
		}
		$text = $result;
		$_SESSION ["administratorConfirmCode"] = $text;
		
		//设置图片的宽度和高度
		$im_x = $w;
		$im_y = $y;
		//创建图片
		$im = imagecreatetruecolor($im_x,$im_y);
		$text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
		$tmpC0=mt_rand(100,255);
		$tmpC1=mt_rand(100,255);
		$tmpC2=mt_rand(100,255);
		$buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
		imagefill($im, 16, 13, $buttum_c);
		
		//字体文件
		$font = _WEB_DIR_.'/font/comic.ttf';
		for ($i=0;$i<strlen($text);$i++){
			$tmp =substr($text,$i,1);
			$array = array(-1,1);
			$p = array_rand($array);
			$an = $array[$p]*mt_rand(1,10);//角度
			$size = 28;
			imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
		}
		
		//将字符写入文件中
		$distortion_im = imagecreatetruecolor ($im_x, $im_y);
		imagefill($distortion_im, 16, 13, $buttum_c);
		for ( $i=0; $i<$im_x; $i++) {
			for ( $j=0; $j<$im_y; $j++) {
				$rgb = imagecolorat($im, $i , $j);
				if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
					imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);
				}
			}
		}
		
		//干扰元素点的数量
		$count = 160;
		//创建干扰元素点
		for($i=0; $i<$count; $i++){
			$randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
			imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
		}
		
		//创建干扰线条
		$rand = mt_rand(5,30);
		$rand1 = mt_rand(15,25);
		$rand2 = mt_rand(5,10);
		for ($yy=$rand; $yy<=+$rand+2; $yy++){
			for ($px=-80;$px<=80;$px=$px+0.1){
				$x=$px/$rand1;
				if ($x!=0){
					$y=sin($x);
				}
				$py=$y*$rand2;
				imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
			}
		
		}
		
		//以PNG格式将图像输出到浏览器
		ImagePNG($distortion_im);
		
		//销毁图像
		ImageDestroy($distortion_im);
		ImageDestroy($im);
	}

	/**
	 * 生成汉字验证码图片
	 * @param int $length 验证码字符长度
	 */
	public function image4($num = '4', $w = 150, $h = 50)
	{
		session_start ();
		//设置文件头为图片输出
		Header("Content-type: image/PNG");
		
		$str = '恭喜发财富贵双全双喜临门五福临门六六大顺一帆风顺十全十美万事如意福寿安康岁岁平安年年有余财源亨通金玉满堂喜气洋洋大吉大利万事如意五福临门财源广进五谷丰登福星高照吉庆有余喜气盈门福满门庭万福临门吉星高照心想事成福满人间喜气临门吉祥如意合家欢乐鹏程万里门迎百福瑞气盈门旭日东升和气生财财源广进出入平安欣欣向荣美满幸福和气致祥鸾凤和鸣喜气盈门招财进宝福星高照福禄寿禧荣华富贵大展鸿图家庭幸福前途无量前程似锦';
		$imgWidth = $w;
		$imgHeight = $h;
		$authimg = imagecreate($imgWidth,$imgHeight);
		//$bkcolor=imagecolorallocate($authimg,250,250,250);
		
		$fontfile = _WEB_DIR_.'/font/msyhbd.ttf';
		$white=imagecolorallocate($authimg,234,185,95);
		imagearc($authimg, 150, 8, 20, 20, 75, 170, $white);
		imagearc($authimg, 180, 7,50, 30, 75, 175, $white);
		imageline($authimg,20,20,180,30,$white);
		imageline($authimg,20,18,170,50,$white);
		imageline($authimg,25,50,80,50,$white);
		
		$noise_num = 800;
		$line_num = 20;
		imagecolorallocate($authimg,0xff,0xff,0xff);
		$rectangle_color=imagecolorallocate($authimg,0xAA,0xAA,0xAA);
		$noise_color=imagecolorallocate($authimg,0x00,0x00,0x00);
		$font_color=imagecolorallocate($authimg,0x00,0x00,0x00);
		$line_color=imagecolorallocate($authimg,0x00,0x00,0x00);
		for($i=0;$i<$noise_num;$i++){
			imagesetpixel($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$noise_color);
		}
		for($i=0;$i<$line_num;$i++){
			imageline($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$line_color);
		}
		$randnum=rand(0,mb_strlen($str,"UTF-8")-4);
		if($randnum%2)$randnum+=1;
		$str = mb_substr($str,$randnum,4,"UTF-8");
		
		$_SESSION ["administratorConfirmCode"] = $str;
		ImageTTFText($authimg, 20, 0, 16, 30, $font_color, $fontfile, $str);
		ImagePNG($authimg);
		ImageDestroy($authimg);
	}
	

	/**
	 * 生成汉字验证码图片
	 * @param int $length 验证码字符长度
	 */
	public function image5($num = '4', $w = 150, $h = 50)
	{
		session_start ();
		//设置文件头为图片输出
		Header("Content-type: image/PNG");
	
		$str = array(
				'1'=>array('鸭仔是?','皱立前'),
				'2'=>array('奶瓶是?','蔡宇平'),
				'3'=>array('罗指导是?','罗曦'),
				'4'=>array('黎总是?','黎亮成'),
				'5'=>array('阿锋是?','梁文锋'),
				'6'=>array('广州在?省','广东'),
				'7'=>array('佛山在?省','广东'),
				'8'=>array('深圳在?省','广东'),
				'9'=>array('东莞在?省','广东'),
				'10'=>array('珠海在?省','广东'),
				'11'=>array('中山在?省','广东'),
				'12'=>array('南海在?市','佛山'),
				'13'=>array('顺德在?市','佛山'),
				'14'=>array('禅城在?市','佛山'),
				'15'=>array('公司在?楼','7'),
				'16'=>array('杭州在?省','浙江'),
				'17'=>array('台州在?省','浙江'),
				'18'=>array('今天是?号',date('d', time())),
				'19'=>array('明天是?号',date('d', time())+1),
				'20'=>array('后天是?号',date('d', time())+2),
				'21'=>array('今年是?年',date('Y', time())));
		$imgWidth = $w;
		$imgHeight = $h;
		$authimg = imagecreate($imgWidth,$imgHeight);
		//$bkcolor=imagecolorallocate($authimg,250,250,250);
	
		$fontfile = _WEB_DIR_.'/font/msyhbd.ttf';
		$white=imagecolorallocate($authimg,234,185,95);
		imagearc($authimg, 150, 8, 20, 20, 75, 170, $white);
		imagearc($authimg, 180, 7,50, 30, 75, 175, $white);
		imageline($authimg,20,20,180,30,$white);
		imageline($authimg,20,18,170,50,$white);
		imageline($authimg,25,50,80,50,$white);
	
		$noise_num = 800;
		$line_num = 20;
		imagecolorallocate($authimg,0xff,0xff,0xff);
		$rectangle_color=imagecolorallocate($authimg,0xAA,0xAA,0xAA);
		$noise_color=imagecolorallocate($authimg,0x00,0x00,0x00);
		$font_color=imagecolorallocate($authimg,0x00,0x00,0x00);
		$line_color=imagecolorallocate($authimg,0x00,0x00,0x00);
		for($i=0;$i<$noise_num;$i++){
			imagesetpixel($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$noise_color);
		}
		for($i=0;$i<$line_num;$i++){
			imageline($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$line_color);
		}
		$randnum=rand(1,21);
		$_SESSION ["administratorConfirmCode"] = $str[$randnum][1];
		ImageTTFText($authimg, 20, 0, 16, 30, $font_color, $fontfile, $str[$randnum][0]);
		
		ImagePNG($authimg);
		ImageDestroy($authimg);
	}
}